<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\MallOrderProduct;
use App\Models\MallRefund;
use App\Services\MallRefundService;
use Illuminate\Http\Request;

class MallRefundController extends BaseController
{

    public function index(Request $request) {
        $input = $request->input();
        $search = ['status' => -1, 'start_at' => '', 'end_at' => ''];
        $order_sn = $request->input('order_sn');
        $refund_sn = $request->input('refund_sn');
        $title = $request->input('title');
        $list = MallRefund::apply($search, $input)->when($order_sn, function ($q) use($order_sn){
            $q->whereHas('order_product', function ($qr) use($order_sn) {
                $qr->where('order_sn', 'like', '%' . $order_sn . '%');
            });
        })->when($title, function ($q) use($title){
            $q->whereHas('product', function ($qr) use($title) {
                $qr->where('title', 'like', '%' . $title . '%');
            });
        })->when($refund_sn, function ($q) use($refund_sn){
            $q->where('refund_sn', 'like', '%' . $refund_sn . '%');
        })->withCertain('product', ['title'])->withCertain('order_product', ['order_sn'])->orderBy('id', 'desc')
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }

    public function agree(Request $request) {
        $this->validate($request, [
            'refund_id' => 'required'
        ]);
        $input = $request->input();
        try {
            $info = MallRefund::find($input['refund_id']);
            if (!$info)
                return $this->failed('退款单号不存在');
            $input['order_product_id'] = $info->order_product_id;
            MallRefundService::handle($input, 'agree');
            return $this->success([], '退款成功');
        } catch (\Exception $e) {
            \Log::error($e);
            return $this->failed($e->getMessage());
        }
    }


    public function refuse(Request $request) {
        $this->validate($request, [
            'refund_id' => 'required'
        ]);
        $input = $request->input();
        try {
            $info = MallRefund::find($input['refund_id']);
            if (!$info)
                return $this->failed('退款单号不存在');
            $input['order_product_id'] = $info->order_product_id;
            MallRefundService::handle($input, 'refuse');
            return $this->success([], '退款成功');
        } catch (\Exception $e) {
            \Log::error($e);
            return $this->failed($e->getMessage());
        }
    }

    public function info(Request $request)
    {
        $this->validate($request, [
            'order_product_id' => 'required'
        ]);
        $input = $request->input();
        $info = MallRefund::withCertain('order_product.address', ['phone', 'contact'])
            ->withCertain('order_product.product', ['title', 'video_url', 'image_url'])->where('order_product_id', $input['order_product_id'])->first();
        if (!$info)
            return $this->failed('退款单号不存在');

        $order_product = MallOrderProduct::withCertain('order', ['paid_at'])->find($input['order_product_id']);
        $info->paid_at = $order_product->order->paid_at;

        return $this->success($info);
    }


}

